package com.open.system.service.impl;

import com.open.system.mapper.LogMapper;
import com.open.system.model.Log;
import com.open.common.model.Page;
import com.open.common.model.Sort;
import com.open.system.model.vo.LogVo;
import com.open.system.service.ILogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 
 *
 * @author 大树03
 * @date 2025-07-04 11:16
 */
@Service(value = "logService")
@Slf4j
public class LogServiceImpl implements ILogService {

	private LogMapper logMapper;

    @Autowired
    public void setLogMapper(LogMapper logMapper) {
        this.logMapper = logMapper;
    }

    @Override
    @Transactional()
    public Boolean save(Log model)  {
        return logMapper.save(model);
    }

    @Override
    @Transactional()
    public Boolean update(Log model)  {
        return logMapper.update(model);
    }

	@Override
	public Page<LogVo> findPage(Page<Log> page, Log item)  {
        Page<LogVo> pageVo = new Page<>();
        BeanUtils.copyProperties(page, pageVo);
        Map<String,Object> map = new HashMap<String, Object>(4);
        int startPoint = (page.getIndex()-1)*page.getSize();
        map.put("orderBy",new Sort());
        map.put("item",item);
        map.put("startPoint",startPoint);
        map.put("endPoint",page.getSize());
        Integer count = logMapper.findPageCount(map);
        if (count > startPoint) {
            pageVo.setItems(logMapper.findPage(map));
        }else {
            pageVo.setItems(new ArrayList<LogVo>());
        }
        pageVo.setTotal(count);
        return pageVo;
    }

	@Override
    public List<Log> findByModel(Log item)  {
        Map<String,Object> map=new HashMap<String, Object>(1);
        map.put("item",item);
        return logMapper.findByModel(map);
    }

	@Override
	public Log findByPk(Long pk)  {
        return logMapper.findByPk(pk);
    }

    @Override
    public Integer countByModel(Log item){
        Map<String,Object> map=new HashMap<String, Object>(1);
        map.put("item",item);
        return logMapper.findPageCount(map);
    }

    @Override
    @Transactional()
    public Boolean remove(List<Long> pks) {
        for (Long pk : pks) {
            logMapper.remove(pk);
        }
        return true;
    }
}